//
//  TabBarBaseViewController.m
//  WisdomZhuHai
//
//  Created by yewei on 13-8-27.
//  Copyright (c) 2013年 wyd. All rights reserved.
//

#import "TabBarBaseViewController.h"
#import "Common.h"
#import "AppDelegate.h"
#import "RootViewController.h"
#import "NewsCellViewController.h"
#import "CommonCellViewController.h"
#import "EmptyViewController.h"
#import "GlobalCellViewController.h"

@interface TabBarBaseViewController ()

@property (nonatomic,assign) UINavigationController* navigationCtrl;

@end

@implementation TabBarBaseViewController

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
//        [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],
//                                                 UITextAttributeTextColor,[UIFont systemFontOfSize:17],
//                                                 UITextAttributeFont, nil] forState:UIControlStateNormal];
//        [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],
//                                                 UITextAttributeTextColor,[UIFont systemFontOfSize:17],
//                                                 UITextAttributeFont, nil] forState:UIControlStateSelected];
        self.tabBarItem.image = [Common imageWithColor:[Common colorWithRGB:0xffffff alpha:0]];
//        self.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -12);
        
        if ([Common getIosVersion] < 7.0) {
            [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Common colorWithRGB:0x42afe5],
                                                    UITextAttributeTextColor, nil] forState:UIControlStateSelected];
        }
    }
    return self;
}

-(void)loadView
{
   self.view = [[[UIView alloc]initWithFrame:CGRectZero]autorelease];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
    if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        [self setEdgesForExtendedLayout:UIRectEdgeLeft | UIRectEdgeRight];
    }
#endif
    self.tabBarController.view.backgroundColor = [Common colorWithRGB:0x18aaf6];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent ;
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.layer.shadowOpacity = 0;//去掉阴影
    self.navigationController.navigationBar.layer.shadowColor = [UIColor clearColor].CGColor;
    float screenWidth = [UIScreen mainScreen].bounds.size.width;
    float screenHeight = [UIScreen mainScreen].bounds.size.height;
    float nvBarHeight = self.navigationController.navigationBar.frame.size.height;
    float tabBarHeight = self.tabBarController.tabBar.frame.size.height;
    float statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    float height = screenHeight - (nvBarHeight + tabBarHeight + statusBarHeight);
    self.view.frame = CGRectMake(0, 0, screenWidth, height);
    self.view.backgroundColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1.0];
    self.view.frame = CGRectMake(0, 0, screenWidth, height);
    self.view.backgroundColor = [UIColor whiteColor];
    [Common shareInstance].contentFrame = self.view.frame;
    CGRect frameNoTabBar = self.view.frame;
    frameNoTabBar.size.height += tabBarHeight;
    [Common shareInstance].contentFrameNoTabBar = frameNoTabBar;
    NSString* backName = @"";
    if ([Common getIosVersion] < 7.0) {
        backName = @"返回";
        [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[Common colorWithRGB:0x0F508A]];
    }
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:backName style:UIBarButtonItemStyleBordered target:nil action:nil];

//    UIBarButtonItem *sideViewItem = [[[UIBarButtonItem alloc]initWithTitle:@"More" style:UIBarButtonItemStyleBordered target:self action:@selector(showSideView)] autorelease];
    if([Common getIosVersion] < 7.0)
    {
        UIBarButtonItem *sideViewItem = [[[UIBarButtonItem alloc]initWithTitle:@"更多" style:UIBarButtonItemStyleBordered target:self action:@selector(showSideView)]autorelease];
        self.navigationItem.leftBarButtonItem = sideViewItem;
    }
    else
    {
        UIBarButtonItem *sideViewItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sidebar/sidebar.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showSideView)] autorelease];
        self.navigationItem.leftBarButtonItem = sideViewItem;
    }
    

//    [self.navigationController setHidesBarsOnSwipe:YES];
    //[Common imageWithColor:[Common colorWithRGB:0x0F508A]]
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"sidebar/nav@2x.png"] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationCtrl.navigationBar setBackgroundImage:[Common imageWithColor:[Common colorWithRGB:0xFFFFFF]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
//    [self.navigationController.navigationBar setBackgroundImage:[Common imageWithColor:[Common colorWithRGB:0xFFFFFF]] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setAlpha:0.1f];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    return  UIInterfaceOrientationIsPortrait( interfaceOrientation );
}

// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
- (NSUInteger) supportedInterfaceOrientations{
#ifdef __IPHONE_6_0
    // return UIInterfaceOrientationMaskAllButUpsideDown;
    return UIInterfaceOrientationMaskPortrait;
#endif
}

- (BOOL) shouldAutorotate {
    return NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
//    RootViewController* rootCtrl = appDelegate.viewController;
    //[rootCtrl.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"sidebar/nav@2x.png"] forBarMetrics:UIBarMetricsDefault];
//    rootCtrl.sideViewController.needSwipeShowMenu = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
//    RootViewController* rootCtrl = appDelegate.viewController;
    //[rootCtrl.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"sidebar/nav@2x.png"] forBarMetrics:UIBarMetricsDefault];
//    rootCtrl.sideViewController.needSwipeShowMenu = NO;
}

- (void)showSideView
{
//    AppDelegate *delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
//    YRSideViewController* sideViewCtrl = delegate.viewController.sideViewController;
//    [sideViewCtrl showLeftViewController:YES];
    //类别侧边栏
    NSArray *images = @[
                        [UIImage imageNamed:@"sidebar/news"],
                        [UIImage imageNamed:@"sidebar/food"],
                        [UIImage imageNamed:@"sidebar/live"],
                        [UIImage imageNamed:@"sidebar/walk"],
                        [UIImage imageNamed:@"sidebar/travel"],
                        [UIImage imageNamed:@"sidebar/play"],
                        [UIImage imageNamed:@"sidebar/buy"],
                        [UIImage imageNamed:@"sidebar/sport"]
                        ];
    RNFrostedSidebar *callout = [[RNFrostedSidebar alloc] initWithImages:images];
    callout.delegate = self;
    [callout show];
}

#pragma mark - RNFrostedSidebarDelegate

- (void)sidebar:(RNFrostedSidebar *)sidebar didTapItemAtIndex:(NSUInteger)index {
    NSLog(@"Tapped item at index %lu",(unsigned long)index);
    [sidebar dismiss];
    UIView *view_navagationBar = [[UIView alloc]initWithFrame:CGRectMake(0, -65, 320, 65)];
    view_navagationBar.backgroundColor = [Common colorWithRGB:0x18aaf6];
    CommonCellViewController *cellView = [[CommonCellViewController alloc]init];
    cellView.isDisplayTopMenu = true;
    NSArray* API_REQ = [NSArray arrayWithObjects:V2APISTORES,R_GETSTORES, nil];
    cellView.API_REQ = [NSArray arrayWithArray:API_REQ];
    switch (index) {
        case 0:{
            NewsCellViewController *cellView = [[NewsCellViewController alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height + 50) style:UITableViewStylePlain];
            cellView.title = @"新闻资讯";
            cellView.type = @"NEWS";
            cellView.hidesBottomBarWhenPushed = YES;
            [cellView.view addSubview:view_navagationBar];
            [self.navigationController pushViewController:cellView animated:YES];
            return;
            break;
        }
        case 1:{
            cellView.title = @"食";
            cellView.type = @"FOOD";
            break;
        }
        case 2:{
            cellView.title = @"住";
            cellView.type = @"LIVE";
            break;
        }
        case 3:{
            cellView.title = @"行";
            cellView.type = @"WALK";
            break;
        }
        case 4:{
            cellView.title = @"游";
            cellView.type = @"TRAVEL";
            break;
        }
        case 5:{
            cellView.title = @"娱";
            cellView.type = @"PLAY";
            break;
        }
        case 6:{
            cellView.title = @"购";
            cellView.type = @"BUY";
            break;
        }
        case 7:{
            cellView.title = @"运动";
            cellView.type = @"SPORT";
            break;
        }
        default:
            break;
    }
    cellView.hidesBottomBarWhenPushed = YES;
    [cellView.view addSubview:view_navagationBar];
    [self.navigationController pushViewController:cellView animated:YES];
}

@end
